<?php
namespace Facebook\WebDriver;

use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;

require_once('vendor/autoload.php');
require_once('./fun.php');
require_once('./helper.php');

// start Firefox with 5 second timeout
$host = 'http://localhost:4444/wd/hub'; // this is the default
// $capabilities = DesiredCapabilities::firefox();
$capabilities = DesiredCapabilities::chrome();
$driver = RemoteWebDriver::create($host, $capabilities, 5000);

// navigate to 'http://www.seleniumhq.org/'
$url = 'http://callback.58.com/firewall/valid/988836761.do?'
    .'namespace=chuzulistphp&url=nj.58.com%2Fchuzu%2F0%2F';
$driver->get($url);

// 等网页加载完成
$driver->wait()->until(
    WebDriverExpectedCondition::titleContains('请输入验证码')
);

// 点击验证
$driver->findElement(WebDriverBy::id('btnSubmit'))->click();
sleep(3);

// 等待包含有安全验证的元素, CSS取第一个值
$puzzle = WebDriverBy::id('ISDCaptcha');
$driver->wait()->until(
    WebDriverExpectedCondition::elementTextContains($puzzle, '向右滑动滑块填充拼图')
);


$pos = xpos($driver);

// 按住滑块,拖动
$slider = $driver->findElement(WebDriverBy::className('dvc-slider__handler'));
$driver->action()->clickAndHold($slider)->perform();
$list   = track($pos);
foreach ($list as $step)
    $driver->action()->moveByOffset($step, 0)->perform();
$driver->action()->release($slider)->perform();
usleep(500);

// 等待验证结果
$puzzle = WebDriverBy::id('ISDCaptcha');
$driver->wait()->until(
    WebDriverExpectedCondition::elementTextContains($puzzle, '验证通过')
);


sleep(3);

// close the Firefox
$driver->quit();





// 鼠标轨迹
function track($distance)
{
    # 移动轨迹
    $track = [];
    # 当前位移
    $current = 0;
    # 减速阈值
    $mid = $distance * 4 / 5;
    # 计算间隔
    $t = 0.2;
    # 初速度
    $v = 0;

    while ($current < $distance){
        if ($current < $mid) {
            # 加速度为正2
            $a = 2;
        } else {
            # 加速度为负3
            $a = -2;
        }

        # 初速度v0
        $v0 = $v;
        # 当前速度v = v0 + at
        $v = $v0 + $a * $t;
        # 移动距离x = v0t + 1/2 * a * t^2
        $move = $v0 * $t + 1 / 2 * $a * $t * $t;
        # 当前位移

        if (($current + $move) > $distance) {
            $current = $distance;
        } else {
            $current += $move;
        }
        # 加入轨迹
        $track[]  = round($move);
    }
    return $track;
}


// 取照片
function xpos($driver)
{
    $page = './images/page.png';
    $driver->takeScreenshot($page);

    $bgele  = $driver->findElement(WebDriverBy::className('dvc-captcha__bgImg'));
    $point  = $bgele->getLocation();
    $x = $point->getX();
    $y = $point->getY();
    $size = $bgele->getSize();
    $h = $size->getHeight();
    $w = $size->getWidth();
    $bgpos = [$x, $y, $w, $h];
    $bgpath= './images/bg.png';
    crop_images($page, $bgpos, $bgpath);


    $puzele = $driver->findElement(WebDriverBy::className('dvc-captcha__puzzleImg'));
    $point  = $puzele->getLocation();
    $x = $point->getX();
    $y = $point->getY();
    $size = $puzele->getSize();
    $h = $size->getHeight();
    $w = $size->getWidth();
    $puzpos = [$x, $y, $w, $h];
    $puzpath= './images/puz.png';
    crop_images($page, $puzpos, $puzpath);


    // sleep(10);
    // $driver->quit();
    $x = findX($bgpath);
    if (!$x) {
        echo 'findX faile'.PHP_EOL;
        $driver->quit();
        exit();
    }
    echo "offsetX:{$x}";
    return $x - 11;
}

function findX($path)
{
    $img = imagecreatefrompng($path);
    $w = imagesx($img);
    $h = imagesy($img);

    $isBorder= function(&$res, $x, $y, $diff = 33) {
        $rgb = imagecolorat($res, $x, $y);
        $r = ($rgb >> 16) & 0xFF;
        $g = ($rgb >> 8) & 0xFF;
        $b = $rgb & 0xFF;
        return (abs($r-190) < $diff 
            &&  abs($g-190) < $diff 
            &&  abs($b-190) < $diff);
    };

    for ($i=61; $i < $w; $i++) { 
        for ($j=0; $j < $h; $j++) { 
            $count = 0;
            while ($isBorder($img, $i, $j++) && $j < $h) {
                $count++;
            }

            if ($count >= 15) {
                return $i;
            }
        }
    }

    return false;
}







